<template>
  <div class="cmt-container">
    <h3>发表评论</h3>
    <hr>
    <textarea placeholder="请输入评论" maxlength="120" v-model="msg"></textarea>
    <mt-button type="primary" size="large" @click="sendComment">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item,index) in comments" :key="index">
        <div class="cmt-title">第{{index+1}}楼&nbsp;&nbsp;{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time|dateFormat}}</div>
        <div class="cmt-content">{{item.content}}</div>
      </div>
    </div>
    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      introduction: null,
      comments:[],
      pageSize:1,
      msg:""
    };
  },
  props:['id'],
  methods:{
    getComments(){
      console.log(this.id)
      console.log(this.pageSize)
      this.$http.get('/src/data/comment.json').then(res=>{
        console.log(res)
        this.comments=res.body
      })
    },
    getMore(){
      this.pageSize++
        this.$http.get('/src/data/comment.json').then(res=>{
        this.comments=this.comments.concat(res.body)
      })
    },
    sendComment(){
      if(this.msg.trim()===""){
        this.$alert('请填写评论内容')
        return
      }
      
      //假装post 评论到服务端  哈哈哈
     let json =  {"add_time":Date.now(),"user_name":'哈哈',"content":this.msg.trim()}
      // this.$http.post('/',let).then(res=>{
      //   this.$alert('success')
      // })
      this.comments.unshift(json)
      localStorage.setItem("comments",this.comments)
    }
  },
 mounted() {
   this.getComments()
 },
};
</script>

<style lang="scss" scoped>
.cmt-container {
  h3 {
    font-size: 16px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }
  .cmt-list{
    margin-top:5px;
    .cmt-item{
      .cmt-title{
          background-color: #ccc;
          font-size: 13px;
          line-height: 30px;
      }
      .cmt-content{
          font-size: 14px;
          line-height: 40px;
          text-indent: 2rem;
      }
    }
  }
}
</style>

